package com.hgw.Demo05_PrototypePattern.Exercises7_7_6.ShallowCloning;

import java.io.*;

/**
 * Data time:2021/12/13 12:03
 * StudentID:2019112118
 * Author:hgw
 * Description:
 */
public class Customer implements Serializable {

    private Address address;

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    // 使用序列化技术实现深克隆
    public Customer deepClone() throws IOException, ClassNotFoundException, OptionalDataException {
        // 将对象写入流中
        ByteArrayOutputStream bao = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bao);
        oos.writeObject(this);

        // 将对象从流中取出
        ByteArrayInputStream bis = new ByteArrayInputStream(bao.toByteArray());
        ObjectInputStream ois = new ObjectInputStream(bis);
        return (Customer) ois.readObject();
    }
}
